package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.SysRoleDTO;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;

    @GetMapping("/list")
    public TableDataInfo list(SysRoleDTO roleDTO) {
        Page<SysRole> page = new Page<>(roleDTO.getPageNum(), roleDTO.getPageSize());
        List<SysRole> list = roleService.list(page);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    @PostMapping
    public AjaxResult add(@RequestBody SysRole role) {
        return toAjax(roleService.save(role));
    }

    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable Long roleId) {
        return AjaxResult.success(roleService.getById(roleId));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody SysRole role) {
        return toAjax(roleService.updateById(role));
    }

    protected AjaxResult toAjax(boolean status) {
        return status ? AjaxResult.success() : AjaxResult.error();
    }
}
